//
//  ReactController.m
//  ocdemo
//
//  Created by mac on 2025/2/9.
//

#import "ReactController.h"
#import "ScriptLoadUtil.h"
#import <React/RCTRootView.h>
#import "BundleDownloadHandler.h"
@interface ReactController ()
@property(nonatomic,strong)UIView * rctView;
@property(nonatomic,strong)NSString * url;
@property(nonatomic,strong)NSString * path;
@property(nonatomic,strong)NSString * name;
@property(nonatomic,assign)BundleType type;
@property(nonatomic,strong)NSDictionary *dataDic;

@end

@implementation ReactController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if([ScriptLoadUtil isDebugable]==NO){
        [self loadScript];
    }
    if(self.type==InApp||[ScriptLoadUtil isScriptLoaded:self.name] ){
        [self initView];
    }
}

- (instancetype)initWithURL:(NSString *)url path:(NSString *)path type:(BundleType)type moduleName:(NSString *)name initialProperties:(nonnull NSDictionary *)dict{
    self=[super init];
    if(self){
        self.url=url;
        self.path=path;
        self.type=type;
        self.name=name;
        self.dataDic=dict;
    }
 
    return  self;
}
-(void)loadScript{
    RCTBridge* bridge=[ScriptLoadUtil getBridge];
    if(self.type==InApp){
        NSString * mainBundlePath=[NSBundle mainBundle].bundlePath;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"BundleLoad" object:nil userInfo:@{@"path":[@"file://" stringByAppendingString:[mainBundlePath stringByAppendingString:@"/"]]}];
        [ScriptLoadUtil loadScriptWithBridge:bridge path:self.path moduleName:self.name mainBundle:true];
    }else if(self.type==NetWork){
        NSString* downloadedBundlePath=[ScriptLoadUtil getDownLoadedScriptDirWithModuleName:self.name];
        if([ScriptLoadUtil isScriptLoaded:self.name]){
            [[NSNotificationCenter defaultCenter] postNotificationName:@"BundleLoad" object:nil userInfo:@{@"path":[@"file://" stringByAppendingString:[downloadedBundlePath stringByAppendingString:@"/"]]}];
            return;
        }
        self.view.backgroundColor=[UIColor whiteColor];
        UIActivityIndicatorView* loadingView=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        loadingView.center=CGPointMake([[UIScreen mainScreen] bounds].size.width/2,200);
        loadingView.color=[UIColor blueColor];
        [self.view addSubview:loadingView];
        [loadingView startAnimating];
        BundleDownloadHandler*downloadHandler=[[BundleDownloadHandler alloc]init];
        [downloadHandler downloadBundle:self.url moduleName:self.name downloadCompleteHandle:^(BOOL success, NSString * _Nonnull bundlePath) {
                    if(success){
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"BundleLoad" object:nil userInfo:@{@"path":[@"file://" stringByAppendingString:[downloadedBundlePath stringByAppendingString:@"/"]]}];
                        
                        //
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            [loadingView stopAnimating];
                            [loadingView removeFromSuperview];
                            [ScriptLoadUtil loadScriptWithBridge:bridge path:self.path moduleName:self.name mainBundle:false];
                            [self initView];
                        }];
                        
                    }
        }];
    }
    
}
-(void)initView{
    RCTBridge*bridge=[ScriptLoadUtil getBridge];
    RCTRootView*view=[[RCTRootView alloc]initWithBridge:bridge moduleName:self.name initialProperties:self.dataDic];
    view.frame=self.view.bounds;
    view.backgroundColor=[UIColor whiteColor];
    self.view=view;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
